package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.AdPosition;
import com.ericom.service.GenericManager;

public class AdPositionAction extends BaseAction {

	private GenericManager<AdPosition, Long> adPositionManager;
	private List adPositions;
	private AdPosition adPosition;
	private Long id;

	public AdPosition getAdPosition() {
		return adPosition;
	}

	public void setAdPosition(AdPosition adPosition) {
		this.adPosition = adPosition;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setAdPositionManager(
			GenericManager<AdPosition, Long> adPositionManager) {
		this.adPositionManager = adPositionManager;
	}

	public String list() {
		adPositions = adPositionManager.getAll();
		return SUCCESS;
	}

	public List getAdPositions() {
		return adPositions;
	}

	public String delete() {
		adPositionManager.remove(adPosition.getId());
		this.saveMessage(getText("adPosition.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			adPosition = adPositionManager.get(id);
		} else {
			adPosition = new AdPosition();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (adPosition.getId() == null);
		adPosition = adPositionManager.save(adPosition);

		String key = (isNew) ? "adPosition.added" : "adPosition.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
